package cn.agiledata.bank.common.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.common.util.WebUtil;
import cn.agiledata.bank.linkbank.action.WebPayRequestAction;
import cn.agiledata.bank.linkbank.logic.payment.UnsPayRequest;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.exception.IPBException;

public class TopAction extends BaseMainAction {
	
	public int hasAccessPermission(int accessLevel, HttpServletRequest request) throws IPBException {
		// TODO Auto-generated method stub
		return 1;
	}
	
	public ActionForward outRoute(ActionMapping mapping, ActionForm actionForm,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		String modual = request.getParameter("modual");
		
		if(WeatherUtil.getLoingInfoFromSession(request) != null) {
			String url = request.getContextPath() + "/route.do?modual=" + modual;
			response.sendRedirect(url);
			
			return mapping.findForward(Constant.FORWORD_NULL);
		}
		
		
		
		
		if(modual != null) {
			
			String[] strings = StringUtils.split(modual, "$");
			if(strings != null) {
				//请求登陆后的资源文件
				if(strings.length > 1) {
					request.setAttribute("showLogin","showLogin"); //用于显示登陆层
//					request.setAttribute("modual",strings[0]); //用于页面主模块的传递
//					request.setAttribute("subModual",modual); //用于登陆后重新定位
					
					ActionForward forward = null;
					if (strings.length==2 && strings[1].equals("merchant")) {
						//此处修改解决了从商户过来的支付请求在未登录状态下，第一次登录失败后再登录成功，则支付信息丢失的问题
						//同时修改了struts-config.xml文件，在/logon的action里配置了名为pay_merchant_failure的forward，在/outRoute的action里配置了名为pay$merchant的forward；
						//还修改了/page/common/main/pay.jsp页，如果是从商户过来的支付请求，则from参数值为merchant，否则为空
						forward = mapping.findForward(modual);
					} else {
						forward = mapping.findForward(strings[0]);
					}
					
//					if(forward == null) {
//						forward = mapping.findForward("main");
//					}
					WebUtil.setSessionString(request,"modual",modual);
					WebUtil.setSessionString(request,"outModual",strings[0]);
					return forward;
				}
//				else {
//					request.setAttribute("modual",modual);
					WebUtil.setSessionString(request,"outModual",modual);
//				}
			}
		}
		

		
		return mapping.findForward(modual);
	}
}
